/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.datasource.pooled;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.ibatis.reflection.ExceptionUtil;

/**
 * @author Clinton Begin
 */
/**
 * 池化的连接
 */
class PooledConnection implements InvocationHandler {

	private static final String CLOSE = "close";
	private static final Class<?>[] IFACES = new Class<?>[] { Connection.class };

	private int hashCode = 0;
	/**
	 * 数据源
	 */
	private PooledDataSource dataSource;
	/**
	 * 真正的连接
	 */
	private Connection realConnection;
	/**
	 * 代理的连接
	 */
	private Connection proxyConnection;
	/**
	 * 当连接池用完时客户端调用getConnection()后等待获取新连接的时间，超时后将抛出SQLException,如设为0则无限期等待。单位毫秒
	 */
	private long checkoutTimestamp;
	/**
	 * 创建时间
	 */
	private long createdTimestamp;
	/**
	 * 最后使用时间
	 */
	private long lastUsedTimestamp;
	/**
	 * 连接类型code
	 */
	private int connectionTypeCode;
	/**
	 * 检查连接
	 */
	private boolean valid;

	/**
	 * Constructor for SimplePooledConnection that uses the Connection and
	 * PooledDataSource passed in (使用连接和池数据传递的Simple EPOLLIDE连接的构造函数)
	 * 
	 * @param connection
	 *            - the connection that is to be presented as a pooled
	 *            connection
	 * @param dataSource
	 *            - the dataSource that the connection is from
	 */
	public PooledConnection(Connection connection, PooledDataSource dataSource) {
		this.hashCode = connection.hashCode();
		this.realConnection = connection;
		this.dataSource = dataSource;
		this.createdTimestamp = System.currentTimeMillis();
		this.lastUsedTimestamp = System.currentTimeMillis();
		this.valid = true;
		 // 创建代理
		this.proxyConnection = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), IFACES, this);
	}

	/**
	 * Invalidates the connection(使连接无效)
	 */
	public void invalidate() {
		valid = false;
	}

	/**
	 * Method to see if the connection is usable (查看连接是否可用。)
	 *
	 * @return True if the connection is usable
	 */
	public boolean isValid() {
		return valid && realConnection != null && dataSource.pingConnection(this);
	}

	/**
	 * Getter for the *real* connection that this wraps(得到真正的连接)
	 *
	 * @return The connection
	 */
	public Connection getRealConnection() {
		return realConnection;
	}

	/**
	 * Getter for the proxy for the connection(得到代理的连接)
	 *
	 * @return The proxy
	 */
	public Connection getProxyConnection() {
		return proxyConnection;
	}

	/**
	 * Gets the hashcode of the real connection (or 0 if it is null)(得到真正的连接的hashCode)
	 *
	 * @return The hashcode of the real connection (or 0 if it is null)
	 */
	public int getRealHashCode() {
		return realConnection == null ? 0 : realConnection.hashCode();
	}

	/**
	 * Getter for the connection type (based on url + user + password)(得到连接类型Code)
	 *
	 * @return The connection type
	 */
	public int getConnectionTypeCode() {
		return connectionTypeCode;
	}

	/**
	 * Setter for the connection type(设置连接类型Code)
	 *
	 * @param connectionTypeCode - the connection type
	 */
	public void setConnectionTypeCode(int connectionTypeCode) {
		this.connectionTypeCode = connectionTypeCode;
	}

	/**
	 * Getter for the time that the connection was created(得到用于创建连接的时间)
	 *
	 * @return The creation timestamp
	 */
	public long getCreatedTimestamp() {
		return createdTimestamp;
	}

	/**
	 * Setter for the time that the connection was created(设置连接的时间)
	 *
	 * @param createdTimestamp - the timestamp
	 */
	public void setCreatedTimestamp(long createdTimestamp) {
		this.createdTimestamp = createdTimestamp;
	}

	/**
	 * Getter for the time that the connection was last used(得到上次使用连接的时间)
	 *
	 * @return - the timestamp
	 */
	public long getLastUsedTimestamp() {
		return lastUsedTimestamp;
	}

	/**
	 * Setter for the time that the connection was last used(设置上次使用连接的时间)
	 *
	 * @param lastUsedTimestamp - the timestamp
	 */
	public void setLastUsedTimestamp(long lastUsedTimestamp) {
		this.lastUsedTimestamp = lastUsedTimestamp;
	}

	/**
	 * Getter for the time since this connection was last used (得到上次使用此连接的时间)
	 *
	 * @return - the time since the last use
	 */
	public long getTimeElapsedSinceLastUse() {
		return System.currentTimeMillis() - lastUsedTimestamp;
	}

	/**
	 * Getter for the age of the connection 得到连接年龄
	 *
	 * @return the age
	 */
	public long getAge() {
		return System.currentTimeMillis() - createdTimestamp;
	}

	/**
	 * Getter for the timestamp that this connection was checked out(得到用于检查此连接的时间戳)
	 *
	 * @return the timestamp
	 */
	public long getCheckoutTimestamp() {
		return checkoutTimestamp;
	}

	/**
	 * Setter for the timestamp that this connection was checked out(设置用于检查此连接的时间戳)
	 *
	 * @param timestamp the timestamp
	 */
	public void setCheckoutTimestamp(long timestamp) {
		this.checkoutTimestamp = timestamp;
	}

	/**
	 * Getter for the time that this connection has been checked out(得到此连接已签出的时间)
	 *
	 * @return the time
	 */
	public long getCheckoutTime() {
		return System.currentTimeMillis() - checkoutTimestamp;
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	/**
	 * Allows comparing this connection to another(允许将此连接与另一个连接进行比较)
	 *
	 * @param obj - the other connection to test for equality
	 * 
	 * @see Object#equals(Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof PooledConnection) {
			return realConnection.hashCode() == (((PooledConnection) obj).realConnection.hashCode());
		} else if (obj instanceof Connection) {
			return hashCode == obj.hashCode();
		} else {
			return false;
		}
	}

	/**
	 * Required for InvocationHandler implementation.(调用处理程序实现所需的)
	 * 代理实现Connection接口的所有方法，只对CLOSE方法特别处理
	 * @param proxy - not used
	 * 
	 * @param method - the method to be executed
	 * 
	 * @param args - the parameters to be passed to the method
	 * 
	 * @see java.lang.reflect.InvocationHandler#invoke(Object,
	 * java.lang.reflect.Method, Object[])
	 */
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName();
		// 如果调用close的话，忽略它，反而将这个connection加入到池中
		if (CLOSE.hashCode() == methodName.hashCode() && CLOSE.equals(methodName)) {
			// 如果为CLOSE方法，那么就将其放回连接池中
			dataSource.pushConnection(this);
			return null;
		} else {
			try {
				if (!Object.class.equals(method.getDeclaringClass())) {
					// issue #579 toString() should never fail
					// throw an SQLException instead of a Runtime
					// 除了toString()方法，其他方法调用之前要检查connection是否还是合法的,不合法要抛出SQLException
					checkConnection();
				}
			    // 其他方法则直接调用实际的连接来处理用
				return method.invoke(realConnection, args);
			} catch (Throwable t) {
				throw ExceptionUtil.unwrapThrowable(t);
			}
		}
	}
	/**
	 * 检查连接
	 * @throws SQLException
	 */
	private void checkConnection() throws SQLException {
		if (!valid) {
			throw new SQLException("Error accessing PooledConnection. Connection is invalid.");
		}
	}

}
